﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.FrameWork.Cache;
using System.Web;
using HMIS.FrameWork;
using System.Xml.Serialization;
using HMIS.FrameWork.DomainModel;

namespace HMIS.MVC.CommonSupport
{
	public class CurrentUser
	{
		public CurrentUser()
		{
			if (!HttpContext.Current.User.Identity.IsAuthenticated)
				throw new DemoHisException("用户未登录");//必须要用户Form验证后才能使用CurrentUser

			var cache = ContainerFactory.GetContainer().Resolve<ICache>();
			if (cache.GetSessionCache("currentuser") == null)
			{
				var authorityFacade = ContainerFactory.GetContainer().Resolve<IAuthorityFacade>();
				UserInfo = authorityFacade.GetHospUserByName(HttpContext.Current.User.Identity.Name);
				MenuPermission = authorityFacade.GetMenusByUserId(UserInfo.Id);
				AllMenuPermission = authorityFacade.GetAllMenusByUserId(UserInfo.Id);
				ActionPermission = authorityFacade.GetActionsByUserId(UserInfo.Id);
				cache.SetSessionCache("currentuser", this);
			}
			else
			{
				var u = (CurrentUser)cache.GetSessionCache("currentuser");
				UserInfo = u.UserInfo;
				MenuPermission = u.MenuPermission;
				AllMenuPermission = u.AllMenuPermission;
				ActionPermission = u.ActionPermission;
			}
		}

		//用户信息
		public Person UserInfo { get; set; }

		//菜单权限
		public IList<MenuNode> MenuPermission { get; set; }

		//菜单权限
		public IList<MenuNode> AllMenuPermission { get; set; }

		//功能权限
		public IList<ActionPermission> ActionPermission { get; set; }

	}

	public class InfraUserService
	{

		public InfraUserInfo UserLogin(string loginName, string userPw)
		{
			InfraUserInfo userInfo = new InfraUserInfo();
			var authorityFacade = ContainerFactory.GetContainer().Resolve<IAuthorityFacade>();
			if (authorityFacade.Validate(loginName, userPw))
			{
				userInfo.UserInfo = authorityFacade.GetHospUserByName(loginName);
				//userInfo.MenuPermission = (List<MenuNode>)authorityFacade.GetMenusByUserId(userInfo.UserInfo.Id);
				//userInfo.AllMenuPermission = (List<MenuNode>)authorityFacade.GetAllMenusByUserId(userInfo.UserInfo.Id);
				//userInfo.ActionPermission = (List<ActionPermission>)authorityFacade.GetActionsByUserId(userInfo.UserInfo.Id);
			}
			else
			{
				userInfo.Erorcd = "-1001";
				userInfo.Erortx = "登录信息有误!";
			}
			return userInfo;
		}

		public SoapRepEntity ChangePwd(string loginName, string oldPwd, string newPwd)
		{
			var authorityFacade = ContainerFactory.GetContainer().Resolve<IAuthorityFacade>();
			HospUser hpuser = authorityFacade.GetHospUserByName(loginName);
			try
			{
				authorityFacade.ChangeUserPwd(hpuser.Id, oldPwd, newPwd);
			}
			catch (Exception ee)
			{
				return new SoapRepEntity("-1001", ee.Message);
			}
			return new SoapRepEntity("0", "密码修改成功");
		}
	}

	[Serializable]
	public class InfraUserInfo
	{
		public string Erorcd { get; set; }

		public string Erortx { get; set; }

		//用户信息
		public HospUser UserInfo { get; set; }

		//菜单权限
		public List<MenuNode> MenuPermission { get; set; }

		//菜单权限
		public List<MenuNode> AllMenuPermission { get; set; }

		//功能权限
		public List<ActionPermission> ActionPermission { get; set; }
	}
}
